<?php

namespace app\planning\controller;
use think\Controller;
use app\common\model\OaWorkersExamModel;
use app\common\model\CustomerModel;
use app\common\model\CustomerRelationModel;
use app\common\service\WorkersService;
use app\common\model\FollowModel;

class Index extends Controller {

	protected $num = 1;
	protected $oa_workers_exam_model;
	protected $customer_model;
	protected $customer_relation_model;
	protected $follow_model;

	public function _initialize() {
		parent::_initialize();
		$this->oa_workers_exam_model = new OaWorkersExamModel;
		$this->customer_model = new CustomerModel();
		$this->customer_relation_model = new CustomerRelationModel();
		$this->follow_model = new FollowModel();
	}

	public function __construct($server = null, $redis = null) {
		parent::__construct();
		$this->server = $server;
		$this->redis = $redis;
	}

	public function index() {
		return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
	}

	//员工考核
	public function workersExam() {
		$list = [];
		//查询所有考核员工
		$lists = $this->oa_workers_exam_model->getAll(['status' => 1, 'end_time' => ['<', time()]], [], 1, -1, 'id,workers_id,start_time,end_time');
		if (empty($lists)) {
			return true;
		}
		$list['type'] = 1;
		$list['data'] = $lists;
		$this->server->task(json_encode($list));
		return true;
	}

	//客户解绑
	public function userUnbind() {
		$list = [];
		//查询所有考核员工
		$lists = $this->customer_model->getAll(['last_be_related_expire_time' => ['=', time(), 'source' => ['>', 0]]], [], 1, -1, 'user_id,source,source_role');
		if (empty($lists)) {
			return true;
		}
		$list['type'] = 2;
		$list['data'] = $lists;
		$this->server->task(json_encode($list));
	}
	//客户解绑提醒
	public function userUnbindWarn(){
		$list = [];
		//查询所有考核员工
		$time = time()+10*86400;//提前10天
		$join = [
			['oa_workers w', 'w.workers_id=a.source', 'left'],
		];
		$lists = $this->customer_model->getAll(['last_be_related_expire_time' => ['=', $time, 'source' => 1]], $join, 1, -1, 'a.user_id,source,source_role,w.mini_openid,w.workers_phone,a.realname,a.telphone,last_be_related_expire_time');
		if (empty($lists)) {
			return true;
		}
		$list['type'] = 4;
		$list['data'] = $lists;
		$this->server->task(json_encode($list));
	}

	//跟进提醒
	public function followWarn() {
		$list = [];
		$join = [
			['oa_workers w', 'w.workers_id=f.workers_id', 'left'],
			['test c', 'c.user_id=f.user_id', 'left']
		];
		$time = time() + 30 * 60; //提前30分钟
		$lists = $this->follow_model->getAll(['next_follow_time' => ['=', $time]], $join, 1, -1, 'f.follow_time,next_follow_time,w.mini_openid,w.workers_phone,c.realname,c.telphone');
		if (empty($lists)) {
			return true;
		}
		$list['type'] = 3;
		$list['data'] = $lists;
		$this->server->task(json_encode($list));
	}

	//员工考核,异步执行
	public function workersExamTask($data) {
		WorkersService::examPosition($data['data']);
	}

	//客户解绑,异步执行
	public function userUnbindTask($data) {
		if (empty($data['data'])) {
			return false;
		}
		foreach ($data['data'] as $k => $v) {
			//重置用户推荐人
			$res = $this->customer_model->edit(['user_id' => $v['user_id']], ['source' => 0, 'last_be_related_expire_time' => 0]);
			//新增客户关联记录
			$record['user_id'] = $v['user_id'];
			$record['parent_role'] = $v['source_role'];
			$record['parent_uid'] = $v['source'];
			$record['type'] = 2;
			$record['create_time'] = time();
			$record['unbind_type'] = 2;
			$res1 = $this->customer_relation_model->add($record);
			if (!$res || !$res1) {
				file_put_contents('./unbind_user_fail.txt', json_encode($record) . PHP_EOL, FILE_APPEND | LOCK_EX); //解绑失败写入文件日志
				continue;
			}
			echo $v['user_id'] . '解绑成功';
		}
	}

	//跟进提醒,异步执行
	public function followWarnTask($data) {
		if (empty($data['data'])) {
			return false;
		}
		foreach ($data['data'] as $k => $v) {
			//短信数据组装
			$sms_tel = $v['workers_phone'];
			$sms_content = '您有客户需要跟进(客户姓名:'.$v['realname'].',客户电话:'.$v['telphone'].')!跟进时间:' . date('Y年m月d日 H:i', $v['next_follow_time']);
			//模板消息数据-公共部分
			$openid = $v['mini_openid'];
			//模板消息数据-小程序
			$weapp_template_data = [
				'keyword1' => ['value' => $v['realname']],
				'keyword2' => ['value' => $v['telphone']],
				'keyword3' => ['value' => date('Y年m月d日 H:i', $v['next_follow_time'])],
			];
			$weapp_template_url = 'pages/home/home';
			$weapp_template_keyword = '';
			//模板ID
			$config = config('work_wx_template');
			$mp_config = config('wechat_template');
			$weapp_template_id = $config['follow_warn'];
			$mp_template_data = [];
			$mp_template_id = '';
			WorkersService::sendNotifiy($openid, $weapp_template_data, $weapp_template_id, $weapp_template_keyword, $sms_tel, $sms_content, $weapp_template_url, $mp_template_id, $mp_template_data);
		}
	}
	//客户解绑,异步执行
	public function userUnbindWarnTask($data) {
		if (empty($data['data'])) {
			return false;
		}
		foreach ($data['data'] as $k => $v) {
			//短信数据组装
			$sms_tel = $v['workers_phone'];
			$sms_content = '您有客户(客户姓名:'.$v['realname'].',客户电话:'.$v['telphone'].')!将在10天后解除绑定';
			//模板消息数据-公共部分
			$openid = $v['mini_openid'];
			//模板消息数据-小程序
			$weapp_template_data = [
				'keyword1' => ['value' => $v['realname'].'(联系电话:'.$v['telphone'].')'],
				'keyword2' => ['value' => '超出系统设置的客户保留时间'],
				'keyword3' => ['value' => date('Y年m月d日 H:i', $v['last_be_related_expire_time'])],
			];
			$weapp_template_url = 'pages/home/home';
			$weapp_template_keyword = '';
			//模板ID
			$config = config('work_wx_template');
			$mp_config = config('wechat_template');
			$weapp_template_id = $config['user_unbind_warn'];
			$mp_template_data = [];
			$mp_template_id = '';
			WorkersService::sendNotifiy($openid, $weapp_template_data, $weapp_template_id, $weapp_template_keyword, $sms_tel, $sms_content, $weapp_template_url, $mp_template_id, $mp_template_data);
		}
	}

}
